package com.omega.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.omega.common.utils.Utils;
import com.omega.entity.ItemDistributed;
import com.omega.service.ItemDenominationService;
import com.omega.service.ItemDistributedService;
import com.omega.service.ItemService;
import com.omega.service.MeasuringUnitService;
import com.omega.service.RetailerService;
import com.omega.vo.EntityListVO;
import com.omega.vo.ItemDistributedListVO;

@Controller
@RequestMapping(value = "/user/itemDistributed")
public class ItemDistributedController {

	@Autowired
	ItemDistributedService itemDistributedService;

	@Autowired
	ItemService itemService;

	@Autowired
	ItemDenominationService itemDenominationService;

	@Autowired
	MeasuringUnitService measuringUnitService;

	@Autowired
	RetailerService retailerService;

	@RequestMapping(value = "/addItemDistributed", method = RequestMethod.GET)
	public String showAddItemDistributedForm(Model model) {
		ItemDistributedListVO itemDistributedListVO = EntityListVO
				.getItemDistributedListVO();
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("itemsList", itemService.loadAllItems());
		model.addAttribute("denominationsList",
				itemDenominationService.loadAllDenominations());
		model.addAttribute("measuringUnitsList",
				measuringUnitService.loadAllMeasuringUnits());
		model.addAttribute("retailerList", retailerService.loadAllRetailers());
		model.addAttribute("itemDistributedListForm", itemDistributedListVO);
		return "addItemDistributed";
	}

	@RequestMapping(value = "/addItemDistributed", method = RequestMethod.POST)
	public String submitAddItemDistributedForm(
			@ModelAttribute("itemDistributedListForm") ItemDistributedListVO itemDistributedListForm,
			BindingResult result, Model model, HttpServletRequest request) {
		for (ItemDistributed form : itemDistributedListForm
				.getItemDistributedList()) {
			if (!(StringUtils.isEmpty(form.getBillNo()))
					& !(StringUtils.isEmpty(form.getItem()))) {
				itemDistributedService.addItemDistributed(form);
			}
		}
		return "redirect:/user/itemDistributed/viewItemsDistributed";
	}

	@RequestMapping(value = "/editItemDistributed/{itemDistributedId}", method = RequestMethod.GET)
	public String showEditItemDistributedForm(
			@PathVariable String itemDistributedId, Model model) {
		ItemDistributed itemDistributed = itemDistributedService.loadById(Long
				.parseLong(itemDistributedId));
		model.addAttribute("itemDistributedForm", itemDistributed);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("itemsList", itemService.loadAllItems());
		model.addAttribute("denominationsList",
				itemDenominationService.loadAllDenominations());
		model.addAttribute("measuringUnitsList",
				measuringUnitService.loadAllMeasuringUnits());
		model.addAttribute("retailerList", retailerService.loadAllRetailers());
		return "editItemDistributed";
	}

	@RequestMapping(value = "/editItemDistributed/{itemDistributedId}", method = RequestMethod.POST)
	public String submitEditItemDistributedForm(
			@ModelAttribute("itemDistributedForm") ItemDistributed form,
			BindingResult result, Model model, HttpServletRequest request) {
		itemDistributedService.updateItemDistributed(form);
		return "redirect:/user/itemDistributed/viewItemsDistributed";
	}

	@RequestMapping(value = "/viewItemsDistributed", method = RequestMethod.GET)
	public String viewAllItemsDistributed(Model model) {
		model.addAttribute("itemDistributedList",
				itemDistributedService.loadAllItemsDistributed());
		return "viewItemsDistributed";
	}

	@RequestMapping(value = "/deleteItemDistributed/{itemDistributedId}", method = RequestMethod.GET)
	public String deleteItemDistributed(
			@PathVariable("itemDistributedId") String itemDistributedId,
			HttpServletRequest request) {
		itemDistributedService.deleteItemDistributed(Long
				.parseLong(itemDistributedId));
		return "redirect:/user/itemDistributed/viewItemsDistributed";
	}

}
